package com.jky.trial.service.impl;

import com.jky.trial.constant.DataStatus;
import com.jky.trial.entity.Relation;
import com.jky.trial.entity.RelationExample;
import com.jky.trial.entity.User;
import com.jky.trial.mapper.RelationMapper;
import com.jky.trial.mapper.UserMapper;
import com.jky.trial.service.MsgService;
import com.jky.trial.service.RelationService;
import com.jky.trial.vo.RelationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class RelationServiceImpl implements RelationService {
    private final RelationMapper recordMapper;
    private UserMapper userMapper;
    private MsgService msgService;


    @Autowired
    RelationServiceImpl(RelationMapper recordMapper, UserMapper userMapper, MsgService msgService) {

        this.userMapper = userMapper;
        this.msgService = msgService;
        this.recordMapper = recordMapper;
    }

    @Override
    public int delTargetId(int targetId, int type) {
        RelationExample userExample = new RelationExample();
        RelationExample.Criteria criteria = userExample.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria.andTargetIdEqualTo(Integer.valueOf(targetId));
        return recordMapper.deleteByExample(userExample);
    }

    @Override
    public int delSourceId(int sourceId, int type) {
        RelationExample userExample = new RelationExample();
        RelationExample.Criteria criteria = userExample.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria.andSourceIdEqualTo(Integer.valueOf(sourceId));
        return recordMapper.deleteByExample(userExample);
    }

    /**
     * 根据targetId,sourceId,type匹配查找
     *
     * @param record
     * @return
     */
    @Override
    public List selectBy(Relation record) {
        RelationExample recordExample = new RelationExample();
        RelationExample.Criteria criteria = recordExample.createCriteria();
        if (record.getType() != null) criteria.andTypeEqualTo(record.getType());
        if (record.getSourceId() != null) criteria.andSourceIdEqualTo(record.getSourceId());
        if (record.getTargetId() != null) criteria.andTargetIdEqualTo(record.getTargetId());
        return recordMapper.selectByExample(recordExample);
    }

    /**
     * 保存单个关系，保存前判断了是否已存在，已存在则拒绝重复保存
     *
     * @param sourceId
     * @param targetId
     * @param type
     * @return
     */
    @Override
    public int save(Integer sourceId, Integer targetId, Integer type) {
        Relation record = new Relation();
        record.setSourceId(sourceId);
        record.setType(type);
        record.setTargetId(targetId);
        User user = userMapper.selectByPrimaryKey(sourceId);
        if (user != null) {
            record.setDepId(user.getDepId());
            record.setBusId(user.getBusId());
            List list = this.selectBy(record);
            if (list.size() != 0) return 0;//关系已存在，直接返回
            return recordMapper.insertSelective(record);
        } else {
            return 0;
        }
    }

    @Override
    public int create(Relation record) {
        List list = this.selectBy(record);
        if (list.size() != 0) return 0;//关系已存在，直接返回
        return recordMapper.insertSelective(record);
    }

    @Override
    public int update(Relation record) {
        RelationExample recordExample = new RelationExample();
        RelationExample.Criteria criteria = recordExample.createCriteria();
        criteria.andIdEqualTo(record.getId());//查询条件
        return recordMapper.updateByExampleSelective(record, recordExample);
    }

    /**
     * 更新关系
     *
     * @param record
     * @return
     */
    @Transactional
    @Override
    public int updateRelation(RelationVo record) {
        List<Integer> list = record.getTargetIds();
        List<Relation> rList = new ArrayList();
        if (list.size() == 0 || record.getSourceId() == null || record.getType() == null) return 0;
        RelationExample recordExample = new RelationExample();
        RelationExample.Criteria criteria = recordExample.createCriteria();
        criteria.andTypeEqualTo(record.getType());
        criteria.andSourceIdEqualTo(record.getSourceId());
        recordMapper.deleteByExample(recordExample); //删除历史数据
        String remarks = record.getRemarks();
        for (int i = 0; i < list.size(); i++) {
            Integer item = list.get(i);
            Relation relation = new Relation();
            relation.setSourceId(record.getSourceId());
            relation.setTargetId(item);
            relation.setType(record.getType());
            if (remarks != null) {
                relation.setRemark(remarks.split("、")[i]);
            }
            if (record.getType().equals(Integer.valueOf(6))) { //实验室人员分配
                if (i == 1 || i == 2) {
                    msgService.sendMsg(DataStatus.MSG_SENDER_SYSTEM.getVal(), item,
                            "分配您为" + (i == 1 ? "初审人" : "审核人"),
                            record.getSourceId(),
                            i == 1 ? DataStatus.MSG_TYPE_TRAIL_ASSIGN_CHECK.getVal() :
                                    DataStatus.MSG_TYPE_TRAIL_ASSIGN_APPROVAL.getVal());
                }

            }
            ;
            rList.add(relation);
        }

        return recordMapper.addBatch(rList);
    }

    @Override
    public int del(int id) {
        return recordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Relation> list(Relation record) {
        if (record.getType() == null) return null;
        ;
        return recordMapper.list(record);
    }

    @Override
    public int batchDel(Relation record) {
        RelationExample recordExample = new RelationExample();
        RelationExample.Criteria criteria = recordExample.createCriteria();
        criteria.andIdEqualTo(record.getId());//查询条件
        criteria.andSourceIdEqualTo(record.getSourceId());//源ID
        criteria.andTypeEqualTo(record.getType()); //数据类型
        return recordMapper.deleteByExample(recordExample);
    }

    @Override
    public int batchInsert(List<Relation> list) {
        return recordMapper.addBatch(list);
    }

    @Override
    public long count(Relation record) {
        RelationExample recordExample = new RelationExample();
        RelationExample.Criteria criteria = recordExample.createCriteria();
        if (record.getTargetId() != null)
            criteria.andTargetIdEqualTo(record.getTargetId());
        if (record.getSourceId() != null)
            criteria.andSourceIdEqualTo(record.getSourceId());
        if (record.getType() != null)
            criteria.andTypeEqualTo(record.getType());
        return recordMapper.countByExample(recordExample);
    }
}
